package com.huobao.yunying.fragments;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.simple.eventbus.EventBus;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.activitys.Act_Search;
import com.huobao.yunying.adapters.Adapter_Fenlei_Ex;
import com.huobao.yunying.adapters.Adapter_Types;
import com.huobao.yunying.entity.Fenlei;
import com.huobao.yunying.entity.FenleiChild;
import com.huobao.yunying.entity.FenleiList;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpConst;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.zhangying.R;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class Fragment_Fenlei1 extends Fragment implements OnItemClickListener,OnClickListener {
	/** 分类的listview */
	private ListView lv_type;
	private ListView listview;
	private Adapter_Fenlei_Ex adapter;
	private Adapter_Types type_Adapter;

	private Http m_Http;
	
	private TextView edt_search;
	private ImageView btn_back;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.layout_fenlei, null);
		findviews(view);
		m_Http = Http.getInstant();
		adapter = new Adapter_Fenlei_Ex(getActivity());
		listview.setAdapter(adapter);
		type_Adapter = new Adapter_Types(getActivity());
		lv_type.setAdapter(type_Adapter);
		lv_type.setOnItemClickListener(this);
		return view;
	}


	private void findviews(View view) {
		btn_back = (ImageView) view.findViewById(R.id.btn_back);
		lv_type = (ListView) view.findViewById(R.id.lv_type);
		listview = (ListView) view.findViewById(R.id.listview);
		edt_search = (TextView) view.findViewById(R.id.edt_search);
		edt_search.setOnClickListener(this);
		btn_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				EventBus.getDefault().post("", "goFristPage");
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		type_Adapter.setSelectPos(position);
		getList(type_Adapter.getList().get(position).getId());
	}

	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);
		if (isVisibleToUser) {
			// 可见时执行的操作
			getTypes();
		} else {
			// 不可见时执行的操作
		}
	}
	
	private void getTypes() {
		Map<String, String> map = HttpUtil.getParams(HttpConst.FRIST_KEY, HttpConst.fenlei);
		m_Http.sendRequest(getActivity(),false, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				List<Fenlei> list = JSON.parseArray(jsonObject.getString("retval"), Fenlei.class);
				type_Adapter.clear();
				type_Adapter.addAll(list);
				getList(null);
			}

			@Override
			public void onHttpError(String error) {
			}
		});
	}

	private void getList(String catid) {
		Map<String, String> map;
		if (catid == null) {
			map = HttpUtil.getParams(HttpConst.FRIST_KEY, HttpConst.fenlei, HttpConst.SECOND_KEY, "catechild");
		} else {
			map = HttpUtil.getParams(HttpConst.FRIST_KEY, HttpConst.fenlei, HttpConst.SECOND_KEY, "catechild", "cateid",
					catid);
		}
		m_Http.sendRequest(getActivity(),false, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				JSONArray jsonArray = jsonObject.getJSONArray("retval");
				List<FenleiList> list = new ArrayList<FenleiList>();
				for (int i = 0; i < jsonArray.size(); i++) {
					JSONObject object = jsonArray.getJSONObject(i);
					FenleiList fenleiList = new FenleiList();
					fenleiList.setId(object.getString("id"));
					fenleiList.setValue(object.getString("value"));
					fenleiList.setChildren(JSON.parseArray(object.getString("children"), FenleiChild.class));
					list.add(fenleiList);
				}
				adapter.clear();
				adapter.addAll(list);
				listview.setSelection(0);
			}

			@Override
			public void onHttpError(String error) {
			}
		});
	}


	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.edt_search:
			Intent intent = new Intent(getActivity(), Act_Search.class);
			getActivity().startActivity(intent);
			break;
		}
	}


}
